﻿using EntityModels.E02.Identities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Security.Claims;

namespace WebAPI.Fileters
{
    public class InitFilter : IAsyncActionFilter
    {
        private readonly UserManager<AppUser> _userManager;
        private readonly ILogger<InitFilter> _logger;

        public InitFilter(UserManager<AppUser> userManager, ILogger<InitFilter> logger)
        {
            _userManager = userManager;
            _logger = logger;
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var id = context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var appUserProp = context.Controller.GetType().GetProperty("AppUserId");
            appUserProp.SetValue(context.Controller, id);
            await next();
        }
    }
}
